package com.lalifa.googledate.ui.activity.login

import android.annotation.SuppressLint
import com.drake.net.utils.scopeNetLife
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.lalifa.base.BaseActivity
import com.lalifa.extension.start
import com.lalifa.googledate.api.UserBean
import com.lalifa.googledate.api.api.InitNet
import com.lalifa.googledate.api.loginApi
import com.lalifa.googledate.databinding.ActivitySplashBinding
import com.lalifa.googledate.ui.activity.MainActivity
import com.lalifa.googledate.ui.dialog.showAgreeDialog
import com.lalifa.googledate.util.CalendarUtils
import com.lalifa.googledate.util.MUtils.Companion.getPerm
import com.lalifa.googledate.util.Tool
import com.lalifa.googledate.util.UserManager
import com.lalifa.utils.SPUtil


@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    override fun getViewBinding() = ActivitySplashBinding.inflate(layoutInflater)

    override fun initView() {
        val sp = SPUtil.getSp(Tool.IS_AGREE, false)
        if (!sp) {
            showAgreeDialog {
                if (it) {
                    SPUtil.setSp(Tool.IS_AGREE, true)
                    getPerm{
                        jump()
                    }
                } else {
                    finish()
                }
            }
        }else{
            getPerm{
                jump()
            }
        }
    }

    private fun jump(){
        //检查是否有登录用户
        val account = GoogleSignIn.getLastSignedInAccount(this@SplashActivity)
        if (account != null) {
            CalendarUtils.CALENDARS_NAME = account.email.pk()
            scopeNetLife {
              //  val loginApi = loginApi(account.idToken.pk())
               // val token = if(loginApi.data==null) "" else loginApi.data!!.userinfo.token
                UserManager.save(
                    UserBean(
                        account.id.pk(),
                        "",
                        account.idToken.pk(),
                        account.account!!.type,
                        if (account.photoUrl == null) "" else account.photoUrl.toString(),
                        account.displayName.pk(),
                        account.familyName.pk(),
                        account.email.pk(),
                        account.account!!.name
                    )
                )
                InitNet.initNetHttp(this@SplashActivity, "")
                start(MainActivity::class.java)
                finish()
            }
        }else{
            start(MainActivity::class.java)
            finish()
        }

    }


    // 获取FB需要的密钥散列
//    private fun facebookSign() {
//        try {
//            val info = packageManager
//                .getPackageInfo(packageName, PackageManager.GET_SIGNATURES)
//            for (signature in info.signatures) {
//                val md = MessageDigest.getInstance("SHA")
//                md.update(signature.toByteArray())
//                LogUtils.i(Base64.encodeToString(md.digest(), Base64.DEFAULT))
//            }
//        } catch (e: NameNotFoundException) {
//            LogUtils.i("facebook key NameNotFoundException")
//        } catch (e: NoSuchAlgorithmException) {
//            LogUtils.i("facebook key NoSuchAlgorithmException")
//        }
//    }
}